/*
 * Copyright 2008 Christopher Davies
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.textish.tokenizer;

import java.util.ArrayList;
import java.util.List;

/**
 * Builds a composite {@link IToken} from a list of other {@link IToken}s.
 * 
 * @author cgdavies
 * 
 */
public abstract class TokenBuilder {

	private List m_childTokens;

	public TokenBuilder() {
		m_childTokens = new ArrayList();
	}

	/**
	 * Adds an {@link IToken} to the token being built.
	 * 
	 * @param token the token to append
	 */
	public void append(IToken token) {
		m_childTokens.add(token);
	}

	/**
	 * Builds a token from the list of appended tokens and then clears that list
	 * ready to build a new token.
	 * 
	 * @return a composite token based on the appended tokens, or
	 * <code>null</code> if no child tokens have been appended
	 */
	public IToken build() {
		if (m_childTokens.isEmpty())
			return null;

		IToken ret = build(m_childTokens);
		m_childTokens = new ArrayList();

		return ret;
	}

	/**
	 * Builds a specific composite token from a list of child tokens.
	 * 
	 * @param childTokens the list of child tokens to build the composite token
	 * from
	 * @return a specific composite token built from the list of child tokens
	 */
	protected abstract IToken build(List childTokens);

}
